package kcms.message.action;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import kcms.db.Course;
import kcms.db.Message;
import kcms.db.Messagestatus;
import kcms.db.PageBean;
import kcms.db.PptAndContent;
import kcms.db.TeachStudent;
import kcms.db.User;
import kcms.db.ViewAssignScore;
import kcms.db.ViewMessage;
import kcms.message.service.MessageService;
import kcms.ppt.service.PptService;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Controller("MessageAction")
@Scope("prototype")
public class MessageAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Resource
    private MessageService messageservice;
	@Resource
	private PptService pptservice;
	//接收<s:checkBoxList>传回来的参数
	private String[] checkCname;
	//接收<s:checkBox>传回的字符串
	private String checkClass;
	//文本框参数
	private String descMessage;
	//当前用户ID
	private String id;
	private String sid;
	//消息id
	private int  messageid;
	private Message message;
	//条件
	private String checkCondition;
	//与数据库对应的视图的list
	private List<TeachStudent> teachStudents;
	private List<TeachStudent> teachStudents1;
	private List<Message> messages;
	private List<Message> messages1;
	private List<Messagestatus> messageStatuses;
	private List<Course> courses;
	private Course course;
	private PageBean pageBean; //封装了分页信息和数据内容的pageBean
	private int page = 1; //表示从网页中返回的当前页的值  默认为1 表示默认显示第一页内容
	private List<ViewMessage> viewMessages;
	private List<ViewMessage> viewMessages1;
	private ViewMessage vmessage;
	private Messagestatus messageStatus;
	private List<PptAndContent> pac;
	private List<ViewAssignScore> vas;
	private ViewAssignScore va;
	private String assignmentid;
	private String cname;
	//list的长度
	private int listSize;
	private String condition;
	private String pageSize="5";
	
	
	public String getAssignmentid() {
		return assignmentid;
	}
	public void setAssignmentid(String assignmentid) {
		this.assignmentid = assignmentid;
	}
	public ViewAssignScore getVa() {
		return va;
	}
	public void setVa(ViewAssignScore va) {
		this.va = va;
	}
	public List<ViewAssignScore> getVas() {
		return vas;
	}
	public void setVas(List<ViewAssignScore> vas) {
		this.vas = vas;
	}
	
	public String getCname() {
		return cname;
	}
	public void setCname(String cname) {
		this.cname = cname;
	}
	public List<PptAndContent> getPac() {
		return pac;
	}
	public void setPac(List<PptAndContent> pac) {
		this.pac = pac;
	}
	public String getSid() {
		return sid;
	}
	public void setSid(String sid) {
		this.sid = sid;
	}

	public Message getMessage() {
		return message;
	}
	public void setMessage(Message message) {
		this.message = message;
	}
	public List<ViewMessage> getViewMessages1() {
		return viewMessages1;
	}
	public void setViewMessages1(List<ViewMessage> viewMessages1) {
		this.viewMessages1 = viewMessages1;
	}
	public Messagestatus getMessageStatus() {
		return messageStatus;
	}
	public void setMessageStatus(Messagestatus messageStatus) {
		this.messageStatus = messageStatus;
	}
	public ViewMessage getVmessage() {
		return vmessage;
	}
	public void setVmessage(ViewMessage vmessage) {
		this.vmessage = vmessage;
	}


	public String getPageSize() {
		return pageSize;
	}
	public void setPageSize(String pageSize) {
		this.pageSize = pageSize;
	}
	public PageBean getPageBean() {
		return pageBean;
	}
	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public String getCondition() {
		return condition;
	}
	public void setCondition(String condition) {
		this.condition = condition;
	}
	public String getCheckCondition() {
		return checkCondition;
	}
	public void setCheckCondition(String checkCondition) {
		this.checkCondition = checkCondition;
	}
	public int getListSize() {
		return listSize;
	}
	public void setListSize(int listSize) {
		this.listSize = listSize;
	}
	public String[] getCheckCname() {
		return checkCname;
	}
	public void setCheckCname(String[] checkCname) {
		this.checkCname = checkCname;
	}
	public String getCheckClass() {
		return checkClass;
	}
	public void setCheckClass(String checkClass) {
		this.checkClass = checkClass;
	}
	public String getDescMessage() {
		return descMessage;
	}
	public void setDescMessage(String descMessage) {
		this.descMessage = descMessage;
	}
	public List<TeachStudent> getTeachStudents() {
		return teachStudents;
	}
	public void setTeachStudents(List<TeachStudent> teachStudents) {
		this.teachStudents = teachStudents;
	}
	public List<TeachStudent> getTeachStudents1() {
		return teachStudents1;
	}
	public void setTeachStudents1(List<TeachStudent> teachStudents1) {
		this.teachStudents1 = teachStudents1;
	}
	
	public MessageService getMessageservice() {
		return messageservice;
	}
	public void setMessageservice(MessageService messageservice) {
		this.messageservice = messageservice;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	
	public List<Message> getMessages() {
		return messages;
	}
	public void setMessages(List<Message> messages) {
		this.messages = messages;
	}
	
	
	public List<Course> getCourses() {
		return courses;
	}
	public void setCourses(List<Course> courses) {
		this.courses = courses;
	}
	public Course getCourse() {
		return course;
	}
	public void setCourse(Course course) {
		this.course = course;
	}
	public int  getMessageid() {
		return messageid;
	}
	public void setMessageid(int messageid) {
		this.messageid = messageid;
	}
	
	public List<Messagestatus> getMessageStatuses() {
		return messageStatuses;
	}
	public void setMessageStatuses(List<Messagestatus> messageStatuses) {
		this.messageStatuses = messageStatuses;
	}
	
	public List<Message> getMessages1() {
		return messages1;
	}
	public void setMessages1(List<Message> messages1) {
		this.messages1 = messages1;
	}
	
	public List<ViewMessage> getViewMessages() {
		return viewMessages;
	}
	public void setViewMessages(List<ViewMessage> viewMessages) {
		this.viewMessages = viewMessages;
	}
	//选择课程
	public String chooseCname(){
		User u=(User) ActionContext.getContext().getSession().get("user");
		teachStudents=messageservice.getAllTeach(u.getId());
		teachStudents1=messageservice.getAllTeach(u.getId());
		//解决字段记录重复出现的问题
		for(int i=0;i<teachStudents.size();i++)
			for(int j=i+1;j<teachStudents.size();j++)
				if(teachStudents.get(i).getId().getCname().equals(teachStudents1.get(j).getId().getCname()))
					{teachStudents1.remove(j);teachStudents.remove(j);j--;}
		return "chooseCname";
	}
	
	
	//选择班级
	public String chooseClass(){
		User u=(User) ActionContext.getContext().getSession().get("user");
		teachStudents=messageservice.getAllTeach(u.getId());
		teachStudents1=messageservice.getAllTeach(u.getId());
		//将checkBoxList选中的课程从对象数组里面挑出来
		if (checkCname!=null) {
			teachStudents.clear();
			int k=0;
			for(int j=0;j<teachStudents1.size();j++){
			for (int i = 0 ; i < checkCname.length ; i ++) {
				if(teachStudents1.get(j).getId().getCid().equals(checkCname[i]))
				 {teachStudents.add(k,teachStudents1.get(j)); k++;}
			}
			}
		}
		return "chooseClass";
	}
	
	
	//添加消息
	public String addMessage(){
		//将chooseClass传递过来的cid和csn分别储存
		User u=(User) ActionContext.getContext().getSession().get("user");
		String ids = this.getCheckClass(); 
		String[] arraycheck = ids.split(", ");
		int length=arraycheck.length;
		String[] cids=new String[length];
		String[] csns=new String[length];
		String[] doubleArray;
		for(int i=0;i<arraycheck.length;i++){
			doubleArray=arraycheck[i].split("!");
			cids[i]=doubleArray[0];
			csns[i]=doubleArray[1];
		}
		//循环插入消息
		for(int i=0;i<cids.length;i++){
			Message m=new Message();
			m.setDescription(getDescMessage());
			m.setReceiverCid(cids[i]);
			m.setReceiverCsn(csns[i]);
			m.setTime(new Date());
			m.setSender(u.getId());
		    this.messageservice.addMessage(m);
		}
		return "showHisMessage";
	}
	
	//显示全部消息
	public String hisMessage(){
		User u=(User) ActionContext.getContext().getSession().get("user");
		messages=messageservice.getHisMessage(u.getId());
		//this.pageBean = messageservice.getForPage(Integer.parseInt(getPageSize()),page,"messageid",u.getId());//获取封装了分页信息和数据的pageBean
		//viewMessages=messageservice.getAllMessagesBySender(sender); //获取数据;
		//viewMessages1=this.pageBean.getList();
		viewMessages=messageservice.getAllMessagesBySender(u.getId());
		viewMessages1=messageservice.getAllMessagesBySender(u.getId());
		for(int i=0;i<viewMessages.size();i++)
			for(int j=i+1;j<viewMessages.size();j++)
				if(viewMessages.get(i).getId().getMessageid().equals(viewMessages1.get(j).getId().getMessageid()))
					{viewMessages1.remove(j);viewMessages.remove(j);j--;}
		//pageSize=getPageSize();
		//page=getPage();
		return "hisMessage";
	}
	//删除指定消息
	public String deleteMessage(){
		messageservice.deleteMessage(getMessageid());
		return "showHisMessage";
	}
	public String showMessageDetails(){
		User u=(User) ActionContext.getContext().getSession().get("user");
		this.message=this.messageservice.getOneMessage(messageid);
		this.vmessage=messageservice.getOneVMessage(messageid,getSid());
		return "showMessageDetails";
	}
	//修改指定消息
	public String modifyMessage(){
		User u=(User) ActionContext.getContext().getSession().get("user");
		this.message.setTime(new Date());
		messageservice.updateMessage(this.message);
		messageStatuses=messageservice.getMessagestatusById(this.message.getMessageid());
		for(int i=0;i<messageStatuses.size();i++){
			//如果已读  false
			if(!messageStatuses.get(i).getStatus())
			{//置为未读
				messageStatuses.get(i).setStatus(true);
				messageservice.updateMessagestatus(messageStatuses.get(i));
			}
		}
		return "showHisMessage";
	}
	//显示未读的消息
	public String notRead(){
		//从messagestatus去找到message里面对应id的消息内容
		User u=(User) ActionContext.getContext().getSession().get("user");
		messageStatus=messageservice.getOneMessageStatusById(getMessageid(),getId());
		messageStatus.setStatus(false);
		messageservice.updateMessageStatus(messageStatus);
		messageStatuses=messageservice.getHisMessagestatus(u.getId());
		vmessage=messageservice.getOneVMessage(getMessageid(),getId());
		return "notRead";
	}
	//显示已读消息
	
	
	public String allMessage(){
		User u=(User) ActionContext.getContext().getSession().get("user");
		if(checkCondition==null||checkCondition.equals(""))
			checkCondition="status";
		this.pageBean = messageservice.getForPage(Integer.parseInt(getPageSize()),page,getCheckCondition(),u.getId());//获取封装了分页信息和数据的pageBean
		viewMessages= this.pageBean.getList(); //获取数据;
		condition=checkCondition;
		pageSize=getPageSize();
		page=getPage();
	    return "allMessage";
	}
	
	
	
	public String orderByCondition(){
		//获得一个condition String
		viewMessages=messageservice.getAllMessagesByCondition(getCheckCondition());
	    return "orderByCondition";
	}
	
	/*public String listPptMessages(){
		User u=(User) ActionContext.getContext().getSession().get("user");
		pac=messageservice.listPptMessages(u.getId());
		System.out.print(pac.size());
		for(int i=0;i<pac.size();i++){
			System.out.println(pac.get(i).getName());
		}
		return "listHisPptMessages";
	}*/
	//只去显示已经提交的，老师已经批改的作业产生的作业消息
	public String listAssignMessages(){
		User u=(User) ActionContext.getContext().getSession().get("user");
		if(checkCondition==null||checkCondition.equals(""))
			checkCondition="2";
		vas=messageservice.listAssignMessages(checkCondition,u.getId());
		System.out.println(vas.size());
		
		for(int i=0;i<vas.size();i++){
			//no assignment submit
		if(vas.get(i).getName()==null&&vas.get(i).getResource()==null)
			{vas.remove(i);i--;}
		else // not read by teacher
			if(vas.get(i).getName()!=null&&vas.get(i).getResource()!=null&&vas.get(i).getPingyu()==null&&vas.get(i).getScore()==null)
				{vas.remove(i);i--;}
		}
		
		listSize=vas.size();
		condition=checkCondition;
		return "listAssignMessages";
	 }
	
	public String assignDetails(){
		va=messageservice.getOneAssignMessage(Integer.parseInt(assignmentid));
		va.setStatus(false);
		messageservice.updateAssignMessage(va);
		return "assignDetails";
	}
	
}
